package com.gomyb.admin.module.user.controller;

import com.gomyb.admin.core.shiro.util.PWD;
import com.gomyb.admin.core.shiro.util.PwdUtil;
import com.gomyb.admin.module.user.model.SysRole;
import com.gomyb.admin.module.user.model.SysUser;
import com.gomyb.admin.module.user.service.SysRoleService;
import com.gomyb.common.base.BaseController;
import com.gomyb.common.base.BaseService;
import com.gomyb.common.page.PageResult;
import com.gomyb.result.JsonResult;
import com.gomyb.result.Result;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author <a href="mailto:HelloHeSir@gmail.com">Mr_He</a>
 * @Copyright (c)</ b> HeC<br/>
 * @createTime 2018/10/29 20:01
 * @Description:
 */
@RestController
@Log4j2
@RequestMapping(value = "/sysUser", name = "后台用户")
public class SysUserController extends BaseController<SysUser, Long> {

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    @Autowired
    public void setService(BaseService<SysUser, Long> service) {
        super.service = service;
    }

    @PostMapping(value = "/see/{id}", name = "查看")
    public Result<SysUser> see(@PathVariable("id") Long id) {
        return super.get(id);
    }

    @PostMapping(value = "/add", name = "添加")
    public Result add(SysUser user, String roles) {
        PWD pwd = PwdUtil.encryptPwd(user.getPassword());
        user.setPassword(pwd.getPassword());
        user.setSalt(pwd.getSalt());
        user = service.save(user);

        Set<SysRole> roleList = user.getRoleList();
        if (null == roleList) {
            roleList = new HashSet<>();
        }
        setRoles(user, roles, roleList);
        return super.save(user);
    }

    @PostMapping(value = "/modify", name = "修改")
    public Result modify(SysUser user, String roles) {
        user = service.get(user.getId());
        Set<SysRole> roleList = new HashSet<>();

        setRoles(user, roles, roleList);
        return super.update(user);
    }

    private void setRoles(SysUser user, String roles, Set<SysRole> roleList) {
        if(StringUtils.isNotEmpty(roles)) {
            String[] roleIds = roles.split(",");
            for (String roleId : roleIds) {
                SysRole sysRole = sysRoleService.get(Long.valueOf(roleId));
                if (null != sysRole) {
                    roleList.add(sysRole);
                }
            }
        }
        user.setRoleList(roleList);
    }

    @PostMapping(value = "/list", name = "列表")
    public PageResult<SysUser> list(HttpServletRequest request) {
        return super.findPage(request);
    }

    @PostMapping(value = "/findRoles", name = "查询全部角色")
    public Result<List<SysRole>> findRoles(HttpServletRequest request) {
        List<SysRole> roles = sysRoleService.findAll();
        return JsonResult.OK(roles);
    }

    @PostMapping(value = "/updatePwd", name = "查询全部角色")
    public Result updatePwd(Long userId, String pwd) {
        SysUser user = service.get(userId);
        PWD pwdd = PwdUtil.encryptPwd(pwd);
        user.setPassword(pwdd.getPassword());
        user.setSalt(pwdd.getSalt());
        return super.update(user);
    }

    @PostMapping(value = "/remove", name = "删除")
    public Result remove(String ids) {
        String[] split = ids.split(",");
        for (String id : split) {
            SysUser user = service.get(Long.valueOf(id));
            super.delete(user);
        }
        return JsonResult.OK();
    }


}